$(function() {
	
	// 左侧导航栏事件
	$('.easyui-accordion').on('click', 'a[name="accordion-nav-a"]', function() {
		var _self = $(this), title = _self.attr('title');
		var mcet = $('#main-content-easyui-tabs');
		if (mcet.tabs('exists', title)) {
			mcet.tabs('select', title)
		} else {
//			$.messager.progress({ text : tab.loadMsg, interval : 88 });
			var _iframeId = 'iframe_' + Math.ceil(Math.random() * 10000000);
			mcet.tabs('add',{
				title: title,
				iconCls : _self.attr('icon'),
				style : { 'padding' : '1px' },
				content: '<iframe id="' + _iframeId + '" src="' + _self.attr('src') + '" frameborder="0" style="border:0;width:100%;height:99.2%;" />',
				closable: true
			});
//			$('#' + _iframeId).load(function() {
//				$.messager.progress('close');
//			});
		}
	});
	
	// 标签右键事件
	$('#main-content-easyui-tabs').tabs({
		onContextMenu : function(e, title, index) {
			e.preventDefault();
			if (index > 0) {
				$('#tabs-menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				}).data("tabTitle", title);
			}
		}
	});
	
	// 标签右键菜单操作
	$("#tabs-menu").menu({
        onClick : function (item) {
            var easyuiTabs = $('#main-content-easyui-tabs');
        	var allTabs = easyuiTabs.tabs('tabs');
        	var allTabtitle = [];
        	$.each(allTabs, function(i, n) {
        		var opt = $(n).panel('options');
        		if (opt.closable)
        			allTabtitle.push(opt.title);
        	});
        	var curTabTitle = $(this).data("tabTitle");
        	var curTabIndex = easyuiTabs.tabs("getTabIndex", easyuiTabs.tabs("getTab", curTabTitle));
        	switch (item.name) {
        		case 1:
        			easyuiTabs.tabs("close", curTabIndex);
        			return false;
        			break;
        		case 2:
        			for (var i = 0; i < allTabtitle.length; i++) {
        				easyuiTabs.tabs('close', allTabtitle[i]);
        			}
        			break;
        		case 3:
        			for (var i = 0; i < allTabtitle.length; i++) {
        				if (curTabTitle != allTabtitle[i])
        					easyuiTabs.tabs('close', allTabtitle[i]);
        			}
        			easyuiTabs.tabs('select', curTabTitle);
        			break;
        		case 4:
        			for (var i = curTabIndex; i < allTabtitle.length; i++) {
        				easyuiTabs.tabs('close', allTabtitle[i]);
        			}
        			easyuiTabs.tabs('select', curTabTitle);
        			break;
        		case 5:
        			for (var i = 0; i < curTabIndex - 1; i++) {
        				easyuiTabs.tabs('close', allTabtitle[i]);
        			}
        			easyuiTabs.tabs('select', curTabTitle);
        			break;
        	}
        }
    });
	
	// 查看用户信息
	$('#easyui-control-menu').find('div[name="viewUserInfo"]').click(function() {
		$.dialog({
			content: 'url:user.do?userInfo',
			min: false,
			max: false,
			lock: true,
			cache:false,
			title : '用户信息',
			width: 300,
		    height: 150
		});
	});
	
	// 修改密码
	$('#easyui-control-menu').find('div[name="changeUserPwd"]').click(function() {
		add('修改密码', 'user.do?changePassword', '', 600, 150);
	});
	
	// 退出登陆
	$('a[name="user-logout-btn"]').click(function() {
		window.top.location.href = 'login.do?logout';
	});
	
	$('div[name="skin-list"]').on('click', $('#easyui-control-menu'), function() {
		var skin = $(this).find('.menu-text').html(), oldSkin = $.cookie('easyui-skin');
		if (oldSkin != skin) {
			$.cookie('easyui-skin', skin, { expires: 356, path: '/' });
			msgTip('皮肤设置完成，请刷新页面生效！');
		}
		
	})
	
});
